{extend name="common/common" /}

{block name="style"}
<style media="screen">
	.treeBox {
		height: 400px;
		overflow: auto;
	}
</style>
{/block}

{block name="content"}
<div class="ibox-title">
    <h5>角色列表
		<div class="top-btn-box">
			{if authCheck('role/authgroupadd')}
			<el-button type="primary" plain size="small" @click="addRow">添加权限组</el-button>
			{/if}
		</div>
	</h5>
</div>
<div class="ibox-content">
	<div id="table-box">
		<el-table :data="tableData" stripe="true">
			<el-table-column prop="id" label="ID" width="50"></el-table-column>
			<el-table-column prop="name" label="权限组名称"></el-table-column>
			<el-table-column prop="operate" label="操作">
				<template slot-scope="scope">
					{if authCheck('role/authgroupedit')}
					<el-button
						@click="editRow(scope.row)"
						size="small" plain type="success"
						icon="el-icon-edit">
					</el-button>
					{/if}
					{if authCheck('role/authgroupdel')}
					<el-button
						@click="delRow(scope.row)"
						size="small" plain 	type="danger"
						icon="el-icon-delete">
					</el-button>
					{/if}
				</template>
			</el-table-column>
		</el-table>
	</div>
</div>
{/block}
{block name="component"}
<div id="nodeTreeBox">
	<el-dialog title="操作权限" :visible.sync="showNodeTree" top="5vh">
		<el-input v-model="name" placeholder="权限组名称"></el-input>
		<div class="treeBox">
			<el-tree
				:data="nodeTree"
				ref="nodeTree"
				show-checkbox
				node-key="id"
				default-expand-all = "true"
				:default-checked-keys="rules">
			</el-tree>
		</div>
		<div slot="footer" class="dialog-footer">
			<el-button @click="showNodeTree = false">取消</el-button>
			<el-button type="success" @click="saveAuthGroup">保存</el-button>
		</div>
	</el-dialog>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
var main = new Vue({
	el:"#main",
	data:function(){
		return {
			loading:true,
			tableData:'',
		}
	},
	mounted:function(){
		this.getTable();
	},
	methods:{
		getTable(){
			this.loading=true;
			$.post("{:url('Role/authGroup')}",function(res){
				main.loading=false;
				if(res.code == 1){
					main.$message({message: res.msg,type:'success'});
					main.tableData = res.data.rows;
				}else{
					main.$notify.error({title: '失败',message: res.msg});
				}
			})
		},
		addRow() {
			nodeTree.name = '';
			nodeTree.showNodeTree = true;
			$.getJSON("{:url('role/giveAccess')}",{'type':'get','id':-1},function(res){
				nodeTree.nodeTree = res.data.node;
				nodeTree.rules = [];
			});
		},
		editRow(row){
			nodeTree.name = row.name;
			nodeTree.showNodeTree = true;
			$.getJSON("{:url('role/getNodeRules')}",{'type':'get','id':row.id},function(res){
				nodeTree.nodeTree = res.data.node;
				var arr = [];
				for(i in res.data.rule){
					arr.push(res.data.rule[i]);
				}
				nodeTree.rules = arr;
			});
		},
		delRow(row){
			this.$confirm('此操作将永久删除该数据,是否继续?','提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			}).then((res) => {
				$.post("{:url('role/authGroupdel')}",{"id":row.id},function(res){
					if(res.code == 1){
						main.$notify({title:"成功",type: 'success',message: res.msg,onClose:main.getTable()});
					}else{
						main.$error({title: '错误'+res.code,message: res.msg});
					}
				})
			}).catch((res) => {
				this.$notify.info({title:"消息",message: "已取消删除"})
			});
		},
	}
})
var nodeTree = new Vue({
	el:"#nodeTreeBox",
	data() {
		return {
			showNodeTree:false,
			nodeTree:'',
			name:"",
			rules:[]
		}
	},
	methods:{
		saveAuthGroup() {
			var AccessArr = this.$refs.nodeTree.getCheckedKeys();
			var HalfArr = this.$refs.nodeTree.getHalfCheckedKeys();
			for(i in HalfArr){
				AccessArr.push(HalfArr[i]);
			}
			var param = {
				name:this.name,rule:AccessArr
			}
			$.post("./authGroupAdd",param,function(res){
				if(res.code == 1){
					nodeTree.showNodeTree = false;
					main.getTable();
					main.$notify({title:"成功",message: res.msg,type: 'success'});
				}else{
					main.$notify.error({title: '失败',message: res.msg});
				}
			})
		}
	}
})
var radioBox = new Vue({
	el:"#radioBox",
	data() {
		return {
			showradioBox:false,
			canSeeType:0,
			editCanseeID:0
		}
	},
	methods:{
		saveSee() {
			$.post("./giveSee",{
				'role_id':radioBox.editCanseeID,
				'type':radioBox.canSeeType
			},function(res){
				if(res.code == 1){
					radioBox.showradioBox = false;
					main.$notify({
						title:"成功",
			          	message: res.msg,
			          	type: 'success'
			        });
				}else{
					main.$notify.error({
						title: '失败',
						message: res.msg,
					});
				}
			})
		}
	}
})
</script>
{/block}
